public class Aaagmnrs {
    
    public boolean isAnagram(String word1, String word2) {
      HashMap<Character, Integer> map = new HashMap<Character, Integer>();
      word1 = word1.toLowerCase();
      word2 = word2.toLowerCase();
      for (Character c: word1.toCharArray()) {
        if (!map.containsKey(c))
          map.put(c, 1);
        else map.put(c, map.get(c)+1);
      }
      
      for (Character c: word2.toCharArray()) {
        if (c == ' ') {
         continue;
         }
        if(map.containsKey(c)) {
         map.put(c, map.get(c)-1);
         if(map.get(c) < 0)
           return false;
         }
         else if (!map.containsKey(c))
           return false;
      }
      
      return true;
    }
    
    public String[] anagrams(String[] phrases) {
      
      ArrayList<String> al = new ArrayList<String>();
      for (String str: phrases)
        al.add(str);
        
      for (int i = 0; i < phrases.length-1; i++) {
        for (int j = i+1; j < phrases.length; j++) {
          if (isAnagram(phrases[i],phrases[j])){
            al.remove(phrases[j]);
          }
        }
      }
      String[] results = new String[al.size()];
      al.toArray(results);
      return results;
    }
  }